<?php

// Namespace

namespace Loop;


/**
 * CHttpHelper allows to call HTTP/S from third-party services via the cURL extension
 *
 * @package		Loop
 * @category	Helper
 */

class CHttpHelper
{
	/**
	 * Does a DELETE query
	 *
	 * @param	string	$url	The URL on which the query must be performed
	 *
	 * @return	string			The result of the query
	 */
	
	public function delete($url)
	{
		// Create the cUrl handler and set it up
		
		$cUrl = curl_init();
		
		curl_setopt($cUrl, CURLOPT_URL, $url);								// The URL
		curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);					// Return the result rather than display it
		curl_setopt($cUrl, CURLOPT_HEADER, false);							// Don't include headers in the result
		curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, false);
		
		
		// Execute the query
		
		$result = curl_exec($cUrl);
		
		
		// Close the HTTP query
		
		curl_close($cUrl);
		
		
		// Has everything worked ok?
		
		if ($result === false)
		{
			e
			(
				EXCEPTION_HTTP_DELETE_QUERY_FAILED,
				array
				(
					'error' => curl_error($cUrl),
					'errorNumber' => curl_errno($cUrl),
					'url' => $url
				)
			);
		}
		
		
		return $result;	
	}
	
	
	/**
	 * Does a GET query
	 *
	 * @param	string	$url	The URL on which the query must be performed
	 *
	 * @return	string			The result of the query
	 */
	
	public function get($url, $variables = null)
	{
		// Create the cUrl handler and set it up
		
		$cUrl = curl_init();
		
		curl_setopt($cUrl, CURLOPT_URL, $url);								// The URL
		curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);					// Return the result rather than display it
		curl_setopt($cUrl, CURLOPT_HEADER, false);							// Don't include headers in the result
		curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, false);
		
		
		// Execute the query
		
		$result = curl_exec($cUrl);
		
		
		// Close the HTTP query
		
		curl_close($cUrl);
		
		
		// Has everything worked ok?
		
		if ($result === false)
		{
			e
			(
				EXCEPTION_HTTP_GET_QUERY_FAILED,
				array
				(
					'error' => curl_error($cUrl),
					'errorNumber' => curl_errno($cUrl),
					'url' => $url
				)
			);
		}
		
		
		return $result;
	}
	
	
	/**
	 * Does a POST query
	 *
	 * @param	string	$url		The URL on which the query must be performed
	 * @param	string	$variables	The variables to send by post, if the array is multi-dimensions encode this one in json
	 *
	 * @return	string			The result of the query
	 */
	
	public function post($url, $variables = array())
	{
		// Create the cUrl handler and set it up
		
		$cUrl = curl_init();
		
		curl_setopt($cUrl, CURLOPT_URL, $url);								// The URL
		curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);					// Return the result rather than display it
		curl_setopt($cUrl, CURLOPT_HEADER, false);							// Don't include headers in the result
		curl_setopt($cUrl, CURLOPT_POST, true);								// It's a POST query
		curl_setopt($cUrl, CURLOPT_POSTFIELDS, $variables);					// POST variables
		curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, false);

		
		// Execute the query
		
		$result = curl_exec($cUrl);
		

		// Close the HTTP query
		
		curl_close($cUrl);
		
		
		// Has everything worked ok?
		
		if ($result === false)
		{
			e
			(
				EXCEPTION_HTTP_POST_QUERY_FAILED,
				array
				(
					'error' => curl_error($cUrl),
					'errorNumber' => curl_errno($cUrl),
					'url' => $url
				)
			);
		}
		

		return $result;
	}
	
	
	/**
	 * Does a PUT query
	 *
	 * @param	string	$url	The URL on which the query must be performed
	 *
	 * @return	string			The result of the query
	 */
	
	public function put($url, $variables = null)
	{
		// Compute the URL
		
		if (is_array($variables) == true)
		{
			$urlFragments = array();
			
			foreach ($variables as $variableName => $variableValue)
			{
				$urlFragments[] = $variableName . '=' . urlencode($variableValue);
			}
			
			$url .= '?' . implode('&', $urlFragments);
		}
		
		
		// Create the cUrl handler and set it up
		
		$cUrl = curl_init();
		
		curl_setopt($cUrl, CURLOPT_URL, $url);								// The URL
		curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);					// Return the result rather than display it
		curl_setopt($cUrl, CURLOPT_HEADER, false);							// Don't include headers in the result
		curl_setopt($cUrl, CURLOPT_CUSTOMREQUEST, 'PUT');					// We do a PUT query
		curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, false);
		
		
		// Execute the query
		
		$result = curl_exec($cUrl);
		
		
		// Close the HTTP query
		
		curl_close($cUrl);
		
		
		// Has everything worked ok?
		
		if ($result === false)
		{
			e
			(
				EXCEPTION_HTTP_PUT_QUERY_FAILED,
				array
				(
					'error' => curl_error($cUrl),
					'errorNumber' => curl_errno($cUrl),
					'url' => $url
				)
			);
		}
		
		
		return $result;
	}
}

?>
